let count = 0;
let intervalId;

self.onmessage = function({data}) {
	if (typeof data === 'object' && data.interval) {
		startTimer(data.interval);
	} else if (data === 'stop') {
		stopTimer();
	}
};

function startTimer(interval) {
	stopTimer(); // 清理任何已经存在的计时器
	count = 0;
	intervalId = setInterval(() => {
		count++;
		self.postMessage(count);
	}, interval);
}

function stopTimer() {
	if (intervalId) {
		clearInterval(intervalId);
		intervalId = null;
	}
}
